package com.bj.disc.ui.splash.vm

import androidx.lifecycle.MutableLiveData
import com.ai3d.rfid.BuildConfig
import com.ai3d.rfid.base.vm.BaseViewModel
import com.ai3d.rfid.common.log.Logcat
import com.ai3d.rfid.common.proxy.MMKVProxy
import com.ai3d.rfid.common.proxy.MMKVProxy.KEY_NEED_DOWNLOAD
import okhttp3.ResponseBody
import retrofit2.Call
import java.util.LinkedList

internal sealed class LauncherViewStatus
internal object PrivacyAgreementViewStatus : LauncherViewStatus() //隐私弹窗状态
internal object StaticZipDownloadState : LauncherViewStatus()              //判断是否需要下载static.zip
internal object StaticZipUnZipStatus : LauncherViewStatus()  //文件解压
internal object JudgeViewState : LauncherViewStatus()              //判断显示页面

/**
 * @ProjectName : Kolb
 * @Author : weijinsong
 * @Time : 2023/1/4 17:58
 * @Description : 启动页
 */
class SplashViewModel : BaseViewModel() {
    private val TAG = "SplashViewModel"
    private val mStepLinkedList = LinkedList<LauncherViewStatus>()
    internal val viewStatusMLD = MutableLiveData<LauncherViewStatus>()
    internal val retry = MutableLiveData<Boolean>()
    var call: Call<ResponseBody>? = null

    fun initStepList() {
        if (checkPrivacy()) {
            mStepLinkedList.add(PrivacyAgreementViewStatus)
        }

        if (MMKVProxy.getBoolean(KEY_NEED_DOWNLOAD, true)) {
            mStepLinkedList.add(StaticZipDownloadState)
            mStepLinkedList.add(StaticZipUnZipStatus)
        }

        mStepLinkedList.add(JudgeViewState)
    }

    /**
     * 拿出一个任务去执行
     */
    fun doNextStep() {
        Logcat.d(TAG, "doNextStep")
        if (mStepLinkedList.isEmpty()) return
        viewStatusMLD.postValue(mStepLinkedList.pop())
    }

    /**
     * 检查是否显示隐私弹窗
     */
    fun checkPrivacy(): Boolean {
        return BuildConfig.SHOW_POPUP_PRIVACY_AGREEMENT && MMKVProxy.getBoolean(
            MMKVProxy.KEY_PRIVACY_AGREE,
            true
        )
    }

    fun userInfo(token: String?) {
        if (token == null)
            return

    }




    private var firstResume = true
    fun onPause() {
        firstResume = false
        call?.cancel()

    }

    fun onResume() {
        if (!firstResume && !checkPrivacy() && !MMKVProxy.getBoolean(
                MMKVProxy.KEY_NEED_NEED_UNZIP,
                false
            )//文件需要解压，则说明下载过，无需下载
        ) {
            Logcat.d(TAG, "onResume")
            mStepLinkedList.clear()
            mStepLinkedList.add(StaticZipDownloadState)
            mStepLinkedList.add(StaticZipUnZipStatus)
            mStepLinkedList.add(JudgeViewState)
            doNextStep()
        }
    }
}